//
//  Select11X5ViewController.m
//  CaiPiao
//
//  Created by apple on 2018/5/22.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "Select11X5ViewController.h"
#import "SelectedNumberView.h"

@interface Select11X5ViewController ()

@property (weak, nonatomic) IBOutlet UIView *numberBoxView;

@property (nonatomic,retain)NSMutableArray* randomNumberArray;
@property (nonatomic,retain)NSMutableArray* seletedNumArray;

@end

@implementation Select11X5ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = [SelectionNumManager shared].currentLotteryName;
    self.seletedNumArray = [NSMutableArray arrayWithCapacity:0];
    self.randomNumberArray  = [NSMutableArray arrayWithCapacity:0];
    [self initNumberBox];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self loadLoaclNumber];
    });
}


-(void)initNumberBox{
    NSArray* numberArray = [[SelectionNumManager shared] redNumberArrayWith:[SelectionNumManager shared].currentLotteryID];
    
    CGFloat ballWidth = 35;
    CGFloat spacingWith = 6;
    NSInteger countBallInRow = self.numberBoxView.frame.size.width/(ballWidth+spacingWith);
    
    __weak typeof(self)weakSelf = self;
    for (int i = 0; i<[numberArray count]; i++) {
        SelectedNumberView* view = [[UINib nibWithNibName:@"NumberView" bundle:[NSBundle mainBundle]] instantiateWithOwner:nil options:nil][0];
        view.frame = CGRectMake((i%countBallInRow)*(ballWidth+spacingWith)+spacingWith, (i/countBallInRow)*(ballWidth+spacingWith), ballWidth, ballWidth);
        view.numberMainColor = [UIColor redColor];
        view.numberLabel.text = numberArray[i];
        view.TapCallback = ^(BOOL selected, NSString *numberText) {
            if (selected) {
                [weakSelf.seletedNumArray addObject:numberText];
            }else{
                [weakSelf.seletedNumArray removeObject:numberText];
            }
        };
        [self.randomNumberArray addObject:view];
        [self.numberBoxView addSubview:view];
    }
}

-(void)clearAction{
    for (SelectedNumberView* view in self.randomNumberArray) {
        [view setNormalState];
    }
}

#pragma mark - 近期开奖
- (IBAction)recentAction:(id)sender {
    UIViewController* viewController = [[UIStoryboard storyboardWithName:@"SelectedNumber" bundle:nil] instantiateViewControllerWithIdentifier:@"RecentWinNumberViewController"];
    
    [viewController setPopinTransitionStyle:BKTPopinTransitionStyleCrossDissolve];
    [viewController setPopinTransitionDirection:BKTPopinTransitionDirectionTop];
    
    CGRect presentationRect = CGRectInset(self.view.bounds, 0, 100);
    [self.navigationController presentPopinController:viewController fromRect:presentationRect animated:YES completion:^{
        NSLog(@"Popin presented !");
    }];
}

#pragma mark - 机选
- (IBAction)randomAction:(id)sender {
    [self clearAction];
    NSArray* numberArray = [[SelectionNumManager shared] randomRedNumberActionWithLottoryID:[SelectionNumManager shared].currentLotteryID];
    for (SelectedNumberView* view in self.randomNumberArray) {
        for (NSString* numberString in numberArray) {
            if ([view.numberLabel.text isEqualToString:numberString]) {
                [view setHighLightState];
            }
        }
    }
}

#pragma mark - 读取本地
-(void)loadLoaclNumber{
    NSArray* localArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"kOfLocal11X5Number"];
    if ([localArray count]>0) {
        for (SelectedNumberView* view in self.randomNumberArray) {
            for (NSString* numberString in localArray) {
                if ([view.numberLabel.text isEqualToString:numberString]) {
                    [view setHighLightState];
                }
            }
        }
    }
    [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"kOfLocal11X5Number"];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

#pragma mark - 删除
- (IBAction)deleteAction:(id)sender {
    [self clearAction];
}

#pragma mark - 确定
- (IBAction)doneAction:(id)sender {
    if ([OpenLoginManager openLoginPageWithController:self]) {
        return;
    }
    
    if ([self.seletedNumArray count]>=5) {
        NSString* numberString = [self.seletedNumArray componentsJoinedByString:@","];
        NSLog(@"%@",numberString);
        CollectionNumberModel *model1 = [CollectionNumberModel setModelWithType:[SelectionNumManager shared].currentLotteryID number:[NSString stringWithFormat:@"%@",numberString]];
        
        [CollectionNumberManager collectionNumberWithArray:@[model1]];
    }else{
        [EasyTextView showErrorText:@"至少选5个号码"];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
